package cn.iocoder.yudao.module.scrm.util;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.scrm.enums.common.OperateTypeEnum;
import cn.iocoder.yudao.module.system.api.dict.DictDataApi;
import cn.iocoder.yudao.module.system.api.dict.dto.DictDataRespDTO;

import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

import static cn.hutool.core.date.DateUtil.formatLocalDateTime;
import static cn.iocoder.yudao.module.scrm.enums.DictTypeConstants.*;

/**
 * 操作记录转换类
 * @Author LiZhiMin
 * @Date 2024/5/23 18:07
 */
@Component
public class OperateRecordUtil {

    @Resource
    private DictDataApi dictDataApi;

    /**
     * 将更改的字段转记录到content中
     * @param updateTextList 记录的内容
     * @param oldObj 旧的对象
     * @param newObj 新的对象
     * @param crmTypes 类型
     */
    public void searchChange(List<String> updateTextList, Map<String, Object> oldObj, Map<String, Object> newObj, String crmTypes) {
        for (String oldKey : oldObj.keySet()) {
            for (String newKey : newObj.keySet()) {
                if (propertiesMap.get(crmTypes).containsKey(oldKey)) {
                    Object oldValue = oldObj.get(oldKey);
                    Object newValue = newObj.get(newKey);
                    //时间类型进行转换
                    if (oldValue instanceof Date) {
                        Date dates = (Date) oldValue;
                        oldValue=  DateUtil.format(dates,"yyyy-MM-dd");
                    }
                    if (newValue instanceof Date) {
                        Date dates = (Date) newValue;
                        newValue=  DateUtil.format(dates,"yyyy-MM-dd");
                    }
                    if (ObjectUtil.isEmpty(oldValue)) {
                        oldValue = "空";
                    }
                    if (ObjectUtil.isEmpty(newValue)) {
                        newValue = "空";
                    }
                    if (newKey.equals(oldKey) && !Objects.equals(oldValue,newValue)) {
                        switch (oldKey) {
                            //设置字典对应的值
                            case "clientQuality":  //客户质量
                                oldValue = this.getLabelByValue(oldValue, SCRM_CLUE_IDEA);
                                newValue = this.getLabelByValue(newValue, SCRM_CLUE_IDEA);
                                break;
                            case "infoSource":  //信息来源
                                oldValue = this.getLabelByValue(oldValue, SCRM_CLUE_INFO);
                                newValue = this.getLabelByValue(newValue, SCRM_CLUE_INFO);
                                break;
                            case "clientSource":  //客户来源
                                oldValue = this.getLabelByValue(oldValue, SCRM_CLUE_CLIENT);
                                newValue = this.getLabelByValue(newValue, SCRM_CLUE_CLIENT);
                                break;
                            // Add more cases as needed
                            default:
                                break;
                        }
                        updateTextList.add( propertiesMap.get(crmTypes).get(oldKey) + "  由  " + oldValue + "  变为  " + newValue + "。");
                    }
                }
            }
        }
    }


    private String getLabelByValue(Object value, String dictType) {
        if (!"空".equals(value.toString())) {
            return dictDataApi.getDictData(dictType, value.toString()).getData().getLabel();
        }
        return value.toString(); // 返回原始值
    }


    /**
     * 根据字典值设置字典的中文名字-循环比远程调用慢吗
     * @param dataList 字典列表
     * @param value 值
     */
    private Object updateValues(List<DictDataRespDTO> dataList, Object value) {
        for (DictDataRespDTO dictData : dataList) {
            if (dictData.getValue().equals(value)) {
                value = dictData.getLabel();
            }
        }
        return value;
    }

    /**
     * 属性kv
     */
    public static Map<String, Dict> propertiesMap = new HashMap<>();

    static {
        propertiesMap.put(OperateTypeEnum.CLUE.getType(), Dict.create()
                .set("name", "姓名").set("enterprise", "企业").set("phone", "电话").set("clueSource", "线索来源推广")
                .set("acquisitionTime", "获客时间").set("clientSource","客户来源").set("materialSource", "素材来源").set("infoSource", "信息来源")
                .set("clientQuality", "客户质量").set("remark", "备注"));
        //TODO 客户模块 属性 待补充 // propertiesMap.put(OperateTypeEnum.CLUE.getType(), Dict.create().set("customerName", "客户名称"));

    }
}
